package com.codingke.codingbookstore.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.utils.AppUtils;
import com.codingke.codingbookstore.vo.User;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;


public class RegisterActivity extends BaseActivity implements View.OnClickListener{

    private Button button3_get_validateNumber,button2_register;
    private EditText editText_phone,editText_password,editText_validate_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        button3_get_validateNumber = (Button) findViewById(R.id.button3_get_validateNumber);
        button2_register = (Button) findViewById(R.id.button2_register);
        editText_phone = (EditText) findViewById(R.id.editText_phone);
        editText_password = (EditText) findViewById(R.id.editText_password);
        editText_validate_number = (EditText) findViewById(R.id.editText_validate_number);
        button3_get_validateNumber.setOnClickListener(this);
        button2_register.setOnClickListener(this);

    }

    /**
     * 返回事件
     * @param view
     */
//    public void backClick(View view){
//        finish();
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button3_get_validateNumber:
                getValidateNumber();
                break;
            case R.id.button2_register:
                submitValidateNumber();
                break;

        }
        AppUtils.hideInputMethod(v);
    }

    /**
     * 提交验证码
     */
    private void submitValidateNumber() {
        String sms_code =  editText_validate_number.getText().toString();
        if(TextUtils.isEmpty(sms_code)){
            showToast("请输入验证码");
            return;
        }
        final String phone =  editText_phone.getText().toString();
        if(TextUtils.isEmpty(phone)){
            showToast("请输入手机号码");
            return;
        }
        final String password =  editText_password.getText().toString();
        if(TextUtils.isEmpty(password)){
            showToast("请输入密码");
            return;
        }
        //提交验证码
        BmobSMS.verifySmsCode(this, phone, sms_code, new VerifySMSCodeListener() {
            @Override
            public void done(BmobException ex) {
                if(ex==null){//短信验证码已验证成功
                    Log.i("bmob", "验证通过");
                    register(phone,password);
                }else{
                    Log.i("bmob", "验证失败：code ="+ex.getErrorCode()+",msg = "+ex.getLocalizedMessage());
                }
            }
        });
    }

    /**
     * 注册
     */
    private void register(String phone,String password) {
        User user = new User();
        user.setUsername(phone);
        user.setMobilePhoneNumber(phone);
        user.setMobilePhoneNumberVerified(true);
        user.setPassword(password);
        user.signUp(this, new SaveListener() {
            @Override
            public void onSuccess() {
                showToast("注册成功");
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                System.out.println(s);
                showToast("注册失败");
            }
        });
        finish();
    }

    /**
     * 获取验证码
     */
    private void getValidateNumber() {
        String phone =  editText_phone.getText().toString();
        if(TextUtils.isEmpty(phone)){
            showToast("请输入手机号码");
            return;
        }
        BmobSMS.requestSMSCode(this, phone, "coding_sms", new RequestSMSCodeListener() {
            @Override
            public void done(Integer smsId, BmobException e) {
                if(e==null){//验证码发送成功
                    Log.i("bmob", "短信id：" + smsId);//用于查询本次短信发送详情
                }
            }
        });
    }
}
